/**
 * Javascript function for the formulas question type
 *    
 * @copyright &copy; 2010 Hon Wai, Lau
 * @author Hon Wai, Lau <lau65536@gmail.com>
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
 */

function formulas_submit(submit_location_id, submit_button_name, subans_track_id, subans_num, qid) {
    var insert_location = document.getElementById(submit_location_id);
    insert_location.innerHTML = "<input name='" + submit_button_name + "' value=''>";
    var subans_tracking = document.getElementById(subans_track_id);
    subans_tracking.value = subans_num;
    var responseform = document.getElementById('responseform');
    responseform.action = responseform.action + '#q' + qid;
    responseform.submit();
}


function formulas_form_correctness(id, checked) {
    var err_names = new Array('Relative error', 'Absolute error');
        var nid = 'correctness[' + id + ']';
        var n = document.getElementsByName(nid)[0];
        if (n == null)  return;
        var bid = 'correctness[' + id + ']_buttons';
        var b = document.getElementById(bid);
        if (b == null) {
            var tmp = document.createElement('div');
            tmp.id = bid;
            b = n.parentNode.appendChild(tmp);
        }
        var use_raw_input = checked;
        if (!use_raw_input) {
            var res = /^\s*(_relerr|_err)\s*(<|==)\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*$/.exec(n.value);
            if (res == null) {
                if (n.value.replace(/^\s+|\s+$/g,"").length == 0) {
                    res = ['','_relerr','<','0.01'];
                    n.value = '_relerr < 0.01';
                }
            }
            if (res == null)
                use_raw_input = true;
            else {
                var s = '<select id="'+bid+'_type" onchange="formulas_form_merge('+id+')">';
                s += '<option value="_relerr"' + (res[1] == '_relerr' ? ' selected="selected"' : '') + '>'+err_names[0]+'</option>';
                s += '<option value="_err"' + (res[1] == '_err'    ? ' selected="selected"' : '') + '>'+err_names[1]+'</option>';
                s += '</select><select id="'+bid+'_op" onchange="formulas_form_merge('+id+')">';
                s += '<option value="<"' + (res[2] == '<' ? ' selected="selected"' : '') + '>&lt</option>';
                s += '<option value="=="' + (res[2] == '==' ? ' selected="selected"' : '') + '>==</option>';
                s += '</select><input id="'+bid+'_tol" value="' + res[3] + '" onchange="formulas_form_merge('+id+')">';
                b.innerHTML = s;
            }
        }
        n.style.display = use_raw_input ? 'block' : 'none';
        b.style.display = use_raw_input ? 'none' : 'block';
}


function formulas_form_merge(id) {
    var nid = 'correctness[' + id + ']';
    var n = document.getElementsByName(nid)[0];
    var bid = 'correctness[' + id + ']_buttons';
    var b = document.getElementById(bid);
    var error_type = document.getElementById(bid+'_type').value;
    var error_op   = document.getElementById(bid+'_op').value;
    var error_val  = document.getElementById(bid+'_tol').value;
    n.value = error_type + ' ' + error_op + ' ' + error_val;
}


function formulas_form_display(id_prefix, id, checked) {
    var n = document.getElementsByName(id_prefix + '[' + id + ']')[0];
    if (n == null)  return;
    n.parentNode.parentNode.style.display = checked ? 'block' : 'none';
}


function formulas_form_create_check_box(id_prefix, i, name, display_name) {
    var id = id_prefix + '_' + name + '_' + i;
    var initial_checked = (document.getElementsByName(name + '[' + i + ']')[0].value.length != 0);
    var ctext = initial_checked ? ' checked="checked" ' : '';
    var s = '<input type="checkbox" onclick="formulas_form_display(\''+name+'\','+i+',this.checked)" '+ctext+' id="'+id+'">';
    s += '<label for="'+id+'"> '+display_name+' </label>';
    formulas_form_display(name, i, initial_checked);
    return s;
}


function formulas_form_simplify() {
    if (document.body.id != 'question-type-formulas')  return;
    var display_names = {
        'vars2': 'Variables (condition)',
        'rawcondition': 'Raw condition',
        'otherrule': 'Other rules',
        'subqtext': 'Subquestion text' ,
        'feedback': 'Feedback '
    };
    
    var i = 0;
    while (true) {
        var n = document.getElementsByName('placeholder' + '[' + i + ']')[0];
        if (n == null)  break;  // break until there is no more box
        var s1 = formulas_form_create_check_box(n.id, i, 'subqtext', display_names['subqtext']);
        var s2 = formulas_form_create_check_box(n.id, i, 'feedback', display_names['feedback']);
        n.parentNode.innerHTML += (s1 + s2);
        
        var n = document.getElementsByName('answermark' + '[' + i + ']')[0];
        var s3 = formulas_form_create_check_box(n.id, i, 'vars2', display_names['vars2']);
        
        var id = n.id + '_condition_' + i;
        formulas_form_correctness(i, false);   // always unchecked
        var s4 = '<input type="checkbox" onclick="formulas_form_correctness('+i+',this.checked)" id="'+id+'">';
        s4 += '<label for="'+id+'"> '+display_names['rawcondition']+' </label>';
        n.parentNode.innerHTML += (s3 + s4);
        
        var n = document.getElementsByName('unitpenalty' + '[' + i + ']')[0];
        var s5 = formulas_form_create_check_box(n.id, i, 'otherrule', display_names['otherrule']);
        n.parentNode.innerHTML +=  s5;
        
        i++;
    }
}


function formulas_form_init() {
    formulas_form_simplify();
    //setTimeout('formulas_form_simplify()',250);
    //setTimeout('setTimeout(\'formulas_form_simplify()\',1000)',450);
}



window.onload = formulas_form_init;
